package com.fjrz.bbxwj.main.video.base

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.app.RZApplication
import com.fjrz.bbxwj.entity.User
import com.fjrz.bbxwj.entity.VideoList
import com.fjrz.bbxwj.main.access.AccessActivity
import com.fjrz.bbxwj.main.host.side.MainUserInfoActivity
import com.scwang.smart.refresh.layout.api.RefreshLayout

class VideoHomeFragment : BaseVideoFragment() {

    companion object {
        private const val EXTRA_NAME_CATE_ID = "EXTRA_NAME_CATE_ID"
        fun newInstance(cateId: Int): VideoHomeFragment {
            val bundle: Bundle = Bundle().apply {
                putInt(EXTRA_NAME_CATE_ID, cateId)
            }
            return VideoHomeFragment().apply { arguments = bundle }
        }
    }

    private val mCateId: Int
        get() = arguments?.getInt(EXTRA_NAME_CATE_ID) ?: 0

    override fun initData() {
        viewModel.videoIndex(mCateId)
    }

    override fun setOnItemChildClickListener(videoList: VideoList, view: View, position: Int) {
        if (RZApplication.token().isNullOrEmpty()) {
            AccessActivity.open(activity = requireActivity())
            return
        }
        super.setOnItemChildClickListener(videoList, view, position)
        if (view.id == R.id.ivControlsHeads) MainUserInfoActivity.open(
            activity = requireActivity(),
            user = if (RZApplication.userInfoMe()?.id == videoList.user.id)
                videoList.user.apply { who = User.Who.USER_ME }
            else videoList.user.apply { who = User.Who.USER_HE }
        )
    }

    override fun setOnRefreshListener(refreshLayout: RefreshLayout) {
        viewModel.videoIndex(mCateId)
    }

    override fun setOnLoadMoreListener(refreshLayout: RefreshLayout) {
        viewModel.videoIndex(mCateId, isLoadMore = true)
    }
}